#include <iostream>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std;
int main() {
    int n;
    scanf("%d", &n);
    string str1, str2, str;
    int p1 = 0, f1 = 0, p2 = 0, f2 = 0;
    int len = 1e6;
    cin >> str1;
    cin >> str2;
    for (int i = 0; i < n; i++) {
        cin >> str;
        if (str == str1) {
            p1 = i;
            f1 = 1;
        } else if (str == str2) {
            p2 = i;
            f2 = 1;
        }
        if(f1&&f2)
        {
            len=min(len,abs(p1-p2));
        }
    }
    if (str1==""||str2==""||f1 == 0 || f2 == 0)
    {
       printf("-1");
       return 0;
    }
    printf("%d", len);
    return 0;
}